// pages/authenCategory/authenCategory.js
//获取应用实例
const app = getApp();
const api = require('../../utils/api');
const constant = require('../../utils/constant');
const storage = require('../../utils/storage');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //获取全局变量 导航栏的高度statusBarHeight
    statusBarHeight: app.globalData.statusBarHeight,
    // 胶囊
    jiaonang: app.globalData.jiaonang, 
    navigationHeight: app.globalData.navigationHeight,
    BASEURLWWW:constant.BASEURLWWW,
    BASEURL:constant.BASEURL,
    authenTab: [
      {
        type: 0,
        title: '个人认证'
      },
      {
        type: 1,
        title: '个体认证'
      },
      {
        type: 2,
        title: '企业认证'
      }
    ],
    tabType: 0,
    radioChecked: false,
    isPopShow: false,
    personalOption:{
      pos_type: null
    }, // 个人认证提交
    enterpriseOption: {
      pos_name: '',
      avatar_url: '',
      pos_type: null,
      papers_name: '',
      papers_no: '',
      papers_url: '',
      notify_day: null
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  // 返回上一级
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  // 选项卡
  tabChange(e){
    let tabType = e.currentTarget.dataset.type
    if(tabType != this.data.tabType){
      if(tabType == 0){}
      this.setData({
        tabType
      })
    }
  },
  // 协议勾选
  radioChange(e){
    this.setData({
      radioChecked: !this.data.radioChecked
    })
  },
  // 显示弹出层
  showPop:function(){
    this.setData({
      isPopShow: true
    })
  },
  // 关闭弹出层
  closePop:function(){
    this.setData({
      isPopShow: false
    })
  },
  // 获取文本框值
  getInputValue(e){
    console.info(e)
    let name = e.currentTarget.dataset.name
    let value = e.detail.value
    let enterpriseOption = this.data.enterpriseOption
    switch(name){
      case 'pos_name':
        enterpriseOption.pos_name = value.trim()
        break
      case 'papers_name':
        enterpriseOption.papers_name = value.trim()
        break
      case 'papers_no':
        enterpriseOption.papers_no = value.trim()
        break
      case 'notify_day':
        enterpriseOption.notify_day = value
        break
    }
    this.setData({
      enterpriseOption
    })
  },
  // 上传图片
  uploadImg(e){
    let that = this
    let type = e.currentTarget.dataset.type
    let imgUrl = ''
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        wx.uploadFile({
          url: `${that.data.BASEURL}/myfile/wechat`, //仅为示例，非真实的接口地址
          filePath: res.tempFilePaths[0],
          name: 'file',
          header:{
            'Authorization': 'Bearer '+ wx.getStorageSync(storage.LOCAL_JWT_KEY),
            'Deviceid': 'loveqing'
          },
          formData: {},
          success (res){
            let jsonObj = JSON.parse(res.data)
            if(jsonObj.code == 0){
              wx.showToast({
                title: '上传成功',
                icon: 'success'
              })
              if(type == 1){
                that.setData({
                  'enterpriseOption.avatar_url': jsonObj.data.uri
                })
              }else{
                that.setData({
                  'enterpriseOption.papers_url': jsonObj.data.uri
                })
              }
            }else{
              wx.showToast({
                title: jsonObj.msg,
                icon: 'error'
              })
            }
            console.log(jsonObj);
          }
        })
      }
    })
  },
  // 提交申请
  submit(){
    let queryform = {}
    if(!this.data.radioChecked){
      wx.showToast({
        title: '请勾选《商户认证协议》',
        icon: 'none'
      })
      return false
    }
    if(this.data.tabType == 0){
      queryform = {...this.data.personalOption}
      queryform.pos_type = this.data.tabType
    }else{
      queryform = {...this.data.enterpriseOption}
      queryform.pos_type = this.data.tabType
      if(queryform.pos_name == ''){
        wx.showToast({
          title: '请填写商户名称',
          icon: 'none'
        })
        return false
      }
      if(queryform.papers_name == ''){
        wx.showToast({
          title: '请填写证件名称',
          icon: 'none'
        })
        return false
      }
      if(queryform.papers_no == ''){
        wx.showToast({
          title: '请填写统一社会信用代码',
          icon: 'none'
        })
        return false
      }
      if(queryform.avatar_url == ''){
        wx.showToast({
          title: '请上传商户头像',
          icon: 'none'
        })
        return false
      }
      if(queryform.papers_url == ''){
        wx.showToast({
          title: '请上传证件照片',
          icon: 'none'
        })
        return false
      }
    }
    console.info(queryform)
    api.posAdd(queryform).then(res => {
      console.info(res)
      if(res.code == 0){
        wx.showToast({
          title: '成功',
          icon: 'success'
        })
        setTimeout(()=>{
          wx.reLaunch({
            url: '/pages/index/index',
          })
        },500)
      }else{
        wx.showToast({
          title: '失败',
          icon: 'error'
        })
      }
    })
  }
})